import {
  Card,
  Breadcrumb,
  Form,
  Button,
  Radio,
  Input,
  Upload,
  Space,
  Select,
  message
} from 'antd'
import { PlusOutlined } from '@ant-design/icons'
import { Link } from 'react-router-dom'
import './index.scss'

import ReactQuill from 'react-quill'
import 'react-quill/dist/quill.snow.css'
import { useEffect, useRef, useState } from 'react'
import { createArticleAPI, getChannelsAPI } from '@/apis/article'

const { Option } = Select

const Publish = () => {
  // 获取频道列表
  const [channels, setChannels] = useState([])

  useEffect(() => {
    // 封装函数 在函数体内调用接口
    const getChannels = async () => {
      const res = await getChannelsAPI()
      setChannels(res.data.channels)
    }
    getChannels()
  }, [])

  // 提交表单
  const onFinish = async (formValue) => {
    // console.log(formValue)
    // 校验封面类型imageType和图片列表imageList是否匹配
    if (imageType !== imageList.length) {
      return message.warning(`封面类型为${imageType}，请上传${imageType}张图片`)
    }
    const { title, content, channel_id } = formValue
    // 按照接口要求的格式处理数据
    const reqData = {
      title: title,
      content: content,
      cover: {
        type: imageType, // 封面类型
        images: imageList.map(item=>item.response.data.url) // 图片列表
      },
      channel_id: channel_id,


    }
    // 调用接口提交
    createArticleAPI(reqData)
  }

  // 上传图片的回调函数
  const [imageList, setImageList] = useState([])
  const onChange = (value)=>{
    // console.log("正在上传中", value);
    setImageList(value.fileList)
    // console.log(imageList);
    
  }

  // 切换图片封面类型
  const [imageType, setImageType] = useState(1)
  const onTypeChange = (e)=>{
    // console.log(e.target.value);
    setImageType(e.target.value)
  }

  return (
    <div className="publish">
      <Card
        title={
          <Breadcrumb items={[
            { title: <Link to={'/'}>首页</Link> },
            { title: '发布文章' },
          ]}
          />
        }
      >
        <Form
          labelCol={{ span: 4 }}
          wrapperCol={{ span: 16 }}
          initialValues={{ type: imageType }}
          onFinish={onFinish}
        >
          <Form.Item
            label="标题"
            name="title"
            rules={[{ required: true, message: '请输入文章标题' }]}
          >
            <Input placeholder="请输入文章标题" style={{ width: 400 }} />
          </Form.Item>
          <Form.Item
            label="频道"
            name="channel_id"
            rules={[{ required: true, message: '请选择文章频道' }]}
          >
            <Select placeholder="请选择文章频道" style={{ width: 400 }}>
              {/* value属性用于存储选择的id作为接口的提交字段 */}
              {channels.map((item) => <Option key={item.id} value={item.id}>{item.name}</Option>)}
            </Select>
          </Form.Item>

          <Form.Item label="封面">
            <Form.Item name="type">
              <Radio.Group onChange={onTypeChange}>
                <Radio value={1}>单图</Radio>
                <Radio value={3}>三图</Radio>
                <Radio value={0}>无图</Radio>
              </Radio.Group>
            </Form.Item>
            {/* listType: 决定选择文件框的外观样式 showUploadList: 控制显示上传列表*/}
            {imageType>0 && <Upload
              listType="picture-card"
              showUploadList
              name='image'
              action="http://geek.itheima.net/v1_0/upload"
              onChange={onChange}
              // maxCount=1时，达到上限后继续上传，替换掉之前的图片
              maxCount={imageType}
            >
              <div style={{ marginTop: 8 }}>
                <PlusOutlined />
              </div>
            </Upload>}
          </Form.Item>

          <Form.Item
            label="内容"
            name="content"
            rules={[{ required: true, message: '请输入文章内容' }]}
          >
            {/* 富文本编辑器*/}
            <ReactQuill theme='snow' className='publish-quill' placeholder='请输入文章内容'></ReactQuill>
          </Form.Item>

          <Form.Item wrapperCol={{ offset: 4 }}>
            <Space>
              <Button size="large" type="primary" htmlType="submit">
                发布文章
              </Button>
            </Space>
          </Form.Item>
        </Form>
      </Card>
    </div>
  )
}

export default Publish